package 学校work.test1201;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//DemoFrame继承自JFrame
public class JFrameTest extends JFrame {

    public JFrameTest() {
        JButton open = new JButton("打开程序");
        JButton close = new JButton("关闭程序");
        JButton destroy = new JButton("退出系统");

        //当按钮点击时,弹出对话框
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //对话框显示
                JOptionPane.showMessageDialog(null, "程序已打开", "打开程序", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //对话框显示
                JOptionPane.showMessageDialog(null, "程序已关闭", "关闭程序", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        destroy.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(1);
            }
        });

        getContentPane().add(open);// 获取内容面板, 并把按钮加入到窗口的内容面板
        getContentPane().add(close);
        getContentPane().add(destroy);

        setLayout(new FlowLayout());//设置窗口的布局为流式布局,从左到右排列
        setTitle("Title");//设置窗口的标题
        setSize(300, 300);//设置窗口的宽高
        setLocationRelativeTo(null);//窗口居中
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击窗口的关闭按钮时,结束
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameTest().setVisible(true);// 创建窗口并设置可见
            }
        });
    }
}